/**
 * 网站设置   
 */

layui.define(['form', 'upload', 'util'], function (exports) {
    var $ = layui.$
        , layer = layui.layer
        , laytpl = layui.laytpl
        , setter = layui.setter
        , view = layui.view
        , admin = layui.admin
        , form = layui.form
        , util = layui.util
        , upload = layui.upload;

    var $body = $('body');

    form.render();

    //网站设置初始化
    function init() {
        $.ajax({
            url: setter.baseConfig.apiHost + '/api/system/getwebsiteconfig' //实际使用请改成服务端真实接口
            , type: 'post'
            , contentType: "application/json"
            //, data: JSON.stringify(data)
            , beforeSend: function (xhr) {
                xhr.setRequestHeader("Authorization", 'Bearer ' + layui.data(setter.tableName)[setter.request.tokenName]);
            }
            , success: function (res) {
                if (res.errorCode != '00000') {
                    layer.msg(res.errorMessage, {
                        offset: '15px'
                        , icon: 2
                        , time: 3000
                    });

                    return false;
                }

                //初始化
                $(':input[name=atOnceUploadFileMaxNumber]').val(res.data.atOnceUploadFileMaxNumber);
                $(':input[name=singleFileMaxLength]').val(res.data.singleFileMaxLength);
                $('#copyright').val(res.data.copyright);
                var isEnablePapaerInvoice = res.data.isEnablePapaerInvoice === 'on';
                if (isEnablePapaerInvoice) {
                    $('input[name="isEnablePapaerInvoice"]').prop('checked', true);
                }
            }
        });
    }
    init();

    //自定义验证
    form.verify({
        nickname: function (value, item) { //value：表单的值、item：表单的DOM对象
            if (!new RegExp("^[a-zA-Z0-9_\u4e00-\u9fa5\\s·]+$").test(value)) {
                return '用户名不能有特殊字符';
            }
            if (/(^\_)|(\__)|(\_+$)/.test(value)) {
                return '用户名首尾不能出现下划线\'_\'';
            }
            if (/^\d+\d+\d$/.test(value)) {
                return '用户名不能全为数字';
            }
        }

        //我们既支持上述函数式的方式，也支持下述数组的形式
        //数组的两个值分别代表：[正则匹配、匹配不符时的提示文字]
        , pass: [
            /^[\S]{6,12}$/
            , '密码必须6到12位，且不能出现空格'
        ]

        //确认密码
        , repass: function (value) {
            if (value !== $('#LAY_password').val()) {
                return '两次密码输入不一致';
            }
        }
    });

    //网站设置
    form.on('submit(set_website)', function (obj) {
        var data = obj.field;
        //提交 Ajax 成功后，关闭当前弹层并重载表格
        $.ajax({
            url: setter.baseConfig.apiHost + '/api/system/savewebsiteconfig' //实际使用请改成服务端真实接口
            , type: 'post'
            , contentType: "application/json"
            , data: JSON.stringify(data)
            , beforeSend: function (xhr) {
                xhr.setRequestHeader("Authorization", 'Bearer ' + layui.data(setter.tableName)[setter.request.tokenName]);
            }
            , success: function (res) {
                if (res.errorCode != '00000') {
                    layer.msg(res.errorMessage, {
                        offset: '15px'
                        , icon: 2
                        , time: 3000
                    });

                    return false;
                }

                //登入成功的提示与跳转
                layer.msg('保存成功', {
                    offset: '15px'
                    , icon: 1
                    , time: 3000
                });
            }
        });

    });

    //  2.9+ 版本可省略 attr 参数，默认读取 lay-on
    util.on({
        backhome: function () {
            location.hash = '/';//返回首页
        }
    });

    //邮件服务
    form.on('submit(set_system_email)', function (obj) {
        layer.alert(layui.util.escape(JSON.stringify(obj.field)));

        //提交修改
        /*
        admin.req({
          url: ''
          ,data: obj.field
          ,success: function(){
            
          }
        });
        */
        return false;
    });

    //设置我的资料
    form.on('submit(setmyinfo)', function (obj) {
        //layer.alert(layui.util.escape(JSON.stringify(obj.field)));

        //提交修改    
        admin.req({
            url: setter.baseConfig.apiHost + '/api/user/saveuserinfo'
            , type: 'post'
            , contentType: "application/json"
            , data: JSON.stringify(obj.field)
            , beforeSend: function (xhr) {
                xhr.setRequestHeader("Authorization", 'Bearer ' + layui.data(setter.tableName)[setter.request.tokenName]);
            }
            , done: function (res) {
                if (res.errorCode != '00000') {
                    layer.msg(res.errorMessage, {
                        offset: '15px'
                        , icon: 2
                        , time: 3000
                    });

                    return false;
                }

                //登入成功的提示与跳转
                layer.msg('修改成功', {
                    offset: '15px'
                    , icon: 1
                    , time: 3000
                }, function () {
                    location.hash = '/';
                    location.reload();
                });
            }
        });

        return false;
    });

    //上传头像
    var avatarSrc = $('#LAY_avatarSrc');
    upload.render({
        url: '/api/upload/'
        , elem: '#LAY_avatarUpload'
        , done: function (res) {
            if (res.status == 0) {
                avatarSrc.val(res.url);
            } else {
                layer.msg(res.msg, { icon: 5 });
            }
        }
    });

    //查看头像
    admin.events.avartatPreview = function (othis) {
        var src = avatarSrc.val();
        layer.photos({
            photos: {
                "title": "查看头像" //相册标题
                , "data": [{
                    "src": src //原图地址
                }]
            }
            , shade: 0.01
            , closeBtn: 1
            , anim: 5
        });
    };


    // 设置密码
    form.on('submit(setmypass)', function (obj) {
        var field = obj.field; // 获得表单字段

        layer.msg('ok');

        // 提交修改
        /*
        admin.req({
          url: ''
          ,data: obj.field
          ,success: function(){
            
          }
        });
        */
        return false;
    });

    // 输出接口
    exports('website', {});
});